package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.iarcuschin.simpleratingbar.SimpleRatingBar;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.ShopDetailtem;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ShopListActivity extends AbsActivity implements View.OnClickListener {
    private ImageView ivBack,ivSearch;
    private EditText etSearch;
    private RecyclerView rvShopList;
    private TextView tvSearchType;

    public final static String SearchStr = "search_str";
    public final static String ActivityID = "activity_id";

    private String searchStr;
    private int activityId,pagesize = 10;

    private ShopAdapter shopAdapter;
    private List<ShopDetailtem> data = new LinkedList<>();

    private int page = 1;
    @Override
    public int setView() {
        return R.layout.activity_shop_list;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        searchStr = intent.getStringExtra(SearchStr);
        activityId = intent.getIntExtra(ActivityID,-1);
        if(TextUtils.isEmpty(searchStr)||activityId == -1){
            ToastUtil.showToast("搜索关键字和店铺ID都为空！！！！");
        }
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        Map<String, Object> param = NetTool.newParams();
        param.put("page",page);
        param.put("pagesize",pagesize);
        param.put("searchStr","");
        param.put("activityId",1);
        NetTool.getApi().getShops(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<ShopDetailtem>>>() {
                    @Override
                    public void onData(BaseResponse<List<ShopDetailtem>> value) {
                        if(data.size()>20){
                            shopAdapter.loadMoreEnd();
                        }else{
                            data.addAll(value.getData());
                            shopAdapter.loadMoreComplete();
                            shopAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }

    private void initLogic() {
        ivBack.setOnClickListener(this);
        etSearch.setFocusable(false);
        etSearch.setOnClickListener(this);

        shopAdapter = new ShopAdapter(R.layout.shop_item,data);
        rvShopList.setAdapter(shopAdapter);
        shopAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ShopDetailtem shopDetailtem = data.get(position);
                shopDetailtem.setId(shopDetailtem.getShopId());
                shopDetailtem.setType(Const.SHOP);
                Const.jumpHandle(mActivity,data.get(position));
            }
        });
        shopAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                requestData();
            }
        },rvShopList);
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivSearch = (ImageView) findViewById(R.id.ic_search);
        ivSearch.setVisibility(View.GONE);
        etSearch =(EditText) findViewById(R.id.et_search);
        Drawable drawable = getResources().getDrawable(R.drawable.ic_search);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        etSearch.setCompoundDrawables(drawable,null,null,null);
        tvSearchType = (TextView) findViewById(R.id.tv_search_type);
        tvSearchType.setVisibility(View.GONE);    rvShopList = (RecyclerView) findViewById(R.id.rv_shop_list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvShopList.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ic_back:
                finish();
                break;
            case R.id.et_search:
                finish();
                break;
        }
    }

    private class ShopAdapter extends BaseQuickAdapter<ShopDetailtem,BaseViewHolder>{
        public ShopAdapter(int layoutResId, @Nullable List<ShopDetailtem> data) {
            super(layoutResId, data);
        }
        @Override
        protected void convert(BaseViewHolder helper, ShopDetailtem item) {
            helper.setText(R.id.tv_shop_name,item.getShopName());
            helper.setText(R.id.tv_shop_send_price,"￥"+item.getShopStartPrice()+"起送 | "+"配送 ￥"+item.getShopSendPrice());
            helper.setText(R.id.tv_shop_distance,item.getShopDistance()+"m | "+item.getShopMinute()+"分钟");
            helper.setText(R.id.tv_shop_detail,item.getShopMark()+"    月销售 "+item.getShopSales()+"单");
            SimpleRatingBar simpleRatingBar = helper.getView(R.id.rb_mark);
            simpleRatingBar.setRating(item.getShopMark());
            Glide.with(mActivity).load(item.getShopImg()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_shop_img));
        }
    }
 }
